package GenerateAdvanced;

//泛型上界 : ?extend
class Food
{

}

class Fruit extends Food{

}

class Apple extends Fruit{

}

class Banana extends Fruit{

}


class Message<T>{
    private T message;
    public T getMessage(){
        return message;
    }
    public void setMessage(T message){
        this.message = message;
    }
}

public class Test {
    public static void main(String[] args) {
        Message<Food> message1 = new Message<>();
        fun(message1);
    }

    public static void fun(Message<? super Fruit> temp){
        temp.setMessage(new Banana());
        temp.setMessage(new Apple());
        //通配符的下界不能直接用Fruit接收,因为无法确定Fruit父类是谁,但是可以用Object接收
        //Fruit fruit = temp.getMessage();
        Object fruit = temp.getMessage();
    }
    












    public static void main1(String[] args) {
        Message<String> message = new Message<>();
        message.setMessage("Haste makes waste");
        fun1(message);

        Message<Integer> message2 = new Message<>();
        message2.setMessage(10000);
        fun1(message2);
    }

    public static void main2(String[] args) {
        Message<Apple> message1 = new Message<>();
        Message<Banana> message2 = new Message<>();
        fun2(message1);
        fun2(message2);
    }

    public static void fun2(Message<? extends Fruit> temp){
        Fruit fruit = temp.getMessage();
          System.out.println(fruit);
    }

    public static void fun1(Message<?>  temp){    //通过使用通配符?来代替参数类型,这样在传参时就可以使用任意类型的参数了
        System.out.println(temp.getMessage());
    }
}
